from selenium.webdriver.common.by import By  # H5选择器
import TEST.Head.Qfunc as Qfunc
import TEST.Head.DriverOn as dF
import path
from path import now

class Copy():
    def __init__(self, port='https://copymanga.site'):
        self.page = dF.Page(1, port=port)
        self.title = ''

    # 重定向
    def redirect(self):
        driver = self.page.driver
        # 重定向窗口
        new_window = driver.window_handles[-1]
        driver.switch_to.window(new_window)
        self.page.driver = driver

    def rank_site(self, rank):
        """=============获取内容"""
        driver = self.page.driver

        # 点击排行内容
        driver.find_element(By.XPATH, f'/html/body/main/div[2]/ul/li[{rank}]/div/div[2]').click()

        Qfunc.wait(1)
        # 重定向窗口
        new_window = driver.window_handles[-1]
        driver.switch_to.window(new_window)
        '''返回内容==============='''
        self.page.driver = driver

    def name_site(self, name):
        """=============获取内容"""
        driver = self.page.driver
        # 向搜索栏填入名称信息
        driver.find_element(By.XPATH, '/html/body/header/div[2]/div/div[8]/div/div/div/div/input').send_keys(name)
        # 点击搜索
        driver.find_element(By.XPATH, '/html/body/header/div[2]/div/div[8]/div/div/div/div/div').click()

        # 重定向窗口
        new_window = driver.window_handles[-1]
        driver.switch_to.window(new_window)
        # 检测作品名称
        for i in range(1, 12):
            title = driver.find_element(By.XPATH,
                                        f'/html/body/main/div[2]/div/div/div[1]/div[{i}]/div[2]/div/a/p').get_attribute(
                'title')
            if title == name:
                driver.find_element(By.XPATH, f'/html/body/main/div[2]/div/div/div[1]/div[{i}]/div[2]/div/a/p').click()
                break
        driver.close()

        # 重定向窗口
        new_window = driver.window_handles[-1]
        driver.switch_to.window(new_window)
        '''返回内容==============='''
        self.page.driver = driver

    def main_page(self):
        """=============获取内容"""
        driver = self.page.driver
        # 获取标题内容
        self.title = driver.find_element(By.XPATH, '/html/body/main/div[1]/div/div[2]/ul/li[1]/h6').get_attribute(
            'title')
        # 点击开始阅读
        driver.find_element(By.XPATH, '/html/body/main/div[1]/div/div[2]/ul/li[8]/a[1]').click()
        # 关闭当前窗口
        driver.close()
        # 重定向窗口
        new_window = driver.window_handles[-1]
        driver.switch_to.window(new_window)
        '''返回内容==============='''
        self.page.driver = driver

    def manga(self, loop):
        datas = []
        driver = self.page.driver
        # 新建文件夹并使路径填入
        ppath = path.Path()
        ppath.fp = now()
        ppath.back()
        ppath.isHere(self.title, 1)

        max_page = driver.find_element(By.XPATH, '/html/body/div[1]/span[2]').text
        for i in range(1, int(max_page)):
            # 模拟下滑骗取ajax数据
            Qfunc.gulu(step=2400)
            # 等待数据加载
            el = Qfunc.if_wait(driver, f'/html/body/div[2]/div/ul/li[{i}]/img')
            # 获取图片url,并填入缓存
            url = el.get_attribute('src')

            if url == 'https://hi77-overseas.mangafuna.xyz/static/websitefree/jpg/loading.jpg':
                i -= 1
                continue

            Qfunc.wait(0.1)
            datas.append(url+'\n')
        # 将缓存内容写入文件
        with open(ppath.fp + str(loop) + '.txt', 'w', encoding='utf-8') as ff:
            for row in datas:
                ff.write(row)

        self.page.driver = driver

    def next_part(self):
        try:
            self.page.driver.find_element(By.XPATH, '/html/body/div[4]/div[4]/a').click()
            return True
        except:
            return False


class Manga():

    def easy_rank(self):

        easy = Copy()
        # 点击rank页内容
        easy.page.driver.find_element(By.XPATH, '/html/body/header/div[2]/div/div[5]/div/a').click()

        for i in range(1, 50):
            easy.rank_site(i)
            easy.main_page()

            part = Qfunc.if_wait(easy.page.driver, '/html/body/h4').text
            title = part[part.find('/') + 1:]
            easy.manga(title)
            while easy.next_part():
                part = Qfunc.if_wait(easy.page.driver, '/html/body/h4').text
                title = part[part.find('/')+1:]
                easy.manga(title)
            easy.page.driver.close()
            easy.redirect()
            Qfunc.wait(3)
        easy.page.driver.quit()

    # def easy_name(self, name):
    #     part = 1
    #     easy = Copy()
    #     easy.name_site(name)
    #     easy.main_page()
    #     easy.manga(part)
    #     while easy.next_part():
    #         part += 1
    #         easy.manga(part)
    #     easy.page.driver.quit()

Manga().easy_rank()